package org.jackysoft.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoOptions;

@Configuration
public class MongoConfig  extends AbstractSpringConfig {

	public @Value("${mongo.dbname}")
	String database;
	public @Value("${mongo.host}")
	String host;
	public @Value("${mongo.port}")
	Integer port = 27017;
	public @Value("${mongo.user}")
	String user;
	public @Value("${mongo.password}")
	String password;

	public @Bean
	Mongo mongo() throws Exception {
		MongoOptions opts = new MongoOptions();
		opts.autoConnectRetry = true;
		opts.connectionsPerHost = 100;
		opts.threadsAllowedToBlockForConnectionMultiplier = 10;
		Mongo mon = new Mongo(host, opts);

		return mon;
	}

	public @Bean
	DB mongoDb() throws Exception {
		Mongo mongo = mongo();
		DB db = mongo.getDB(database);
		return db;
	}
}
